﻿using ProjScan.RecordManager.AssetData;
using ProjScan.ScanRule;
using ProjScan.Tools;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager
{
    internal class MeshRenderingRecordManager : RecordManagerBase<MeshRenderingAssetData>
    {
        public override string TableTag => "art_mesh";

        public override void PrepareAssets()
        {
            datas = new List<MeshRenderingAssetData>();
            string[] source = new string[4]
            {
                "fbx",
                "asset",
                "obj",
                "mesh"
            };
            int num = AssetPathManager.Instance.artPathList.Length;
            for (int i = 0; i < num; i++)
            {
                if (Singleton<ScanInfo>.Instance.isEditor)
                {
                    EditorUtility.DisplayProgressBar("Proj Scan [" + TableTag + "]", "Preparing " + TableTag + "(" + datas.Count + ")...", (float)i * 1f / (float)num);
                }

                string text = AssetPathManager.Instance.artPathList[i];
                string value = Path.GetExtension(text).ToLower().Replace(".", "");
                if (!source.Contains(value))
                {
                    continue;
                }

                ActiveAllRecord();
                if (!CheckAssetPathRules(text))
                {
                    continue;
                }

                ModelImporter modelImporter = AssetImporter.GetAtPath(text) as ModelImporter;
                if (modelImporter != null)
                {
                    Object[] array = AssetDatabase.LoadAllAssetsAtPath(text);
                    if (array == null)
                    {
                        continue;
                    }

                    Object[] array2 = array;
                    foreach (Object @object in array2)
                    {
                        Mesh mesh = @object as Mesh;
                        if (mesh != null)
                        {
                            int iD = AssetPathManager.Instance.GetID(Path.GetDirectoryName(text));
                            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text);
                            MeshRenderingAssetData meshRenderingAssetData = new MeshRenderingAssetData(iD, text, fileNameWithoutExtension, mesh, modelImporter);
                            meshRenderingAssetData.id = datas.Count;
                            ValidateAsset(meshRenderingAssetData);
                            meshRenderingAssetData.Reset();
                            datas.Add(meshRenderingAssetData);
                            if (datas.Count % 100 == 0)
                            {
                                ToolsHelper.GC();
                            }
                        }
                    }

                    continue;
                }

                Object[] array3 = AssetDatabase.LoadAllAssetsAtPath(text);
                if (array3 == null)
                {
                    continue;
                }

                if (array3.Length == 1)
                {
                    Mesh mesh2 = array3[0] as Mesh;
                    if (mesh2 != null)
                    {
                        MeshRenderingAssetData meshRenderingAssetData2 = new MeshRenderingAssetData(AssetPathManager.Instance.GetID(Path.GetDirectoryName(text)), text, mesh2, null);
                        meshRenderingAssetData2.id = datas.Count;
                        ValidateAsset(meshRenderingAssetData2);
                        meshRenderingAssetData2.Reset();
                        datas.Add(meshRenderingAssetData2);
                        if (datas.Count % 100 == 0)
                        {
                            ToolsHelper.GC();
                        }
                    }

                    continue;
                }

                Object[] array4 = array3;
                foreach (Object object2 in array4)
                {
                    Mesh mesh3 = object2 as Mesh;
                    if (!(mesh3 != null))
                    {
                        continue;
                    }

                    int iD2 = AssetPathManager.Instance.GetID(Path.GetDirectoryName(text));
                    string fileNameWithoutExtension2 = Path.GetFileNameWithoutExtension(text);
                    MeshRenderingAssetData meshRenderingAssetData3 = new MeshRenderingAssetData(iD2, text, fileNameWithoutExtension2, mesh3, modelImporter);
                    meshRenderingAssetData3.id = datas.Count;
                    foreach (RecordRule recordRule in recordRules)
                    {
                        if (dicEnableRules[recordRule])
                        {
                            recordRule.Check(meshRenderingAssetData3);
                        }
                    }

                    meshRenderingAssetData3.Reset();
                    datas.Add(meshRenderingAssetData3);
                    if (datas.Count % 100 == 0)
                    {
                        ToolsHelper.GC();
                    }
                }
            }
        }
    }
}